﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using Wicresoft.Framework.Organization;
using JDEduTrain.Framework.Business;
using JDEduTrain.Framework.ViewEntity;
using JDEduTrain.Framework.Entity;

public partial class Page_SelectClassList : BaseAuthenticationPage
{

    public string SearchDefaultText
    {
        get
        {
            return "搜索 班级名称";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }
    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "searchClick();");
        this.ShowLRLayoutButton = false;
        this.ShowUDLayoutButton = false;
        this.ShowSimpleLayoutButton = false;
        this.ShowAdvancedSearch = false;

        this.Title = "班级列表";
        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "ApplyEndTime";
            this.Order.OrderDirection = "DESC";
            BindData();
        }

    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    private void BindData()
    {
        int recordCount = 0;
        DataTable dtClass = BLTrain.GetSelectClassList(0, int.MaxValue, SearchClause, string.Format("{0} {1}", this.Order.OrderColumn, this.Order.OrderDirection), ref recordCount);
        dtClass.Columns.Add("ApplyState");
        DTUserPageList = dtClass.Clone();
        DTUserPageList.Clear();
        recordCount = 0;
        foreach (DataRow dr in dtClass.Rows)
        {
            TrainClass trainClass = new TrainClass();
            trainClass.Init(dr);
            ClassMember member = new ClassMember(CurrentUser.ID, trainClass.ID);
            if (member.Load())
            {
                dr["ApplyState"] = member.Status;
            }
            else
            {
                dr["ApplyState"] = "未报名";
            }
            string[] deptIDs = trainClass.ApplyOrganID.Split(';');
            foreach (string deptID in deptIDs)
            {
                if (!string.IsNullOrEmpty(deptID))
                {
                    if (OrganizationHelper.IsUserInDepartment(CurrentUser.ID, new Guid(deptID)))
                    {
                        DTUserPageList.ImportRow(dr);
                        recordCount++;
                        break;
                    }
                }
            }

        }
        Pager.RecordCount = recordCount;
        DTUserPageList = WebCommon.GetPagedTable(DTUserPageList, Pager.PageIndex + 1, Pager.PageSize);
        DataView dv = DTUserPageList.DefaultView;
        if (this.Order.OrderColumn == "PassTime")
        {
            dv.Sort = "ApplyState" + " " + this.Order.OrderDirection;
        }
        rptUserInfo.DataSource = dv;
        rptUserInfo.DataBind();

    }

    protected void rptUserInfo_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Image imgSign = e.Item.FindControl("imgSign") as Image;
            Guid id = new Guid(((DataRowView)e.Item.DataItem).Row["ID"].ToString());
            TrainClass trainClass = new TrainClass(id);
            trainClass.Load();
            ClassMember member = new ClassMember(CurrentUser.ID, trainClass.ID);
            if (member.Load())
            {
                if (member.Status == "终审通过")
                {
                    imgSign.ImageUrl = "../Images/pass.png";
                }
                else if (member.Status == "初审驳回" || member.Status == "终审驳回")
                {
                    imgSign.ImageUrl = "../Images/fail.png";
                }
                else
                {
                    imgSign.ImageUrl = "../Images/apply.png";
                }
                imgSign.ToolTip = member.Status;
            }
            else
            {
                imgSign.Visible = false;    
            }

        }
    }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause = string.Format("{0} LIKE N'%{1}%'", TrainClass.SQLCOL_CLASSNAME, tbSearch.Text);
        else
            SearchClause = "1=1";
        BindData();
    }
    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        BindData();

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ArrayList alClass = new ArrayList();

        for (int i = 0; i < rptUserInfo.Items.Count; i++)
        {
            if ((rptUserInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptUserInfo.Items[i].ItemIndex]["ID"].ToString());
                alClass.Add(id);
            }
        }
        BLTrain.DeleteTrainClass((Guid[])alClass.ToArray(typeof(Guid)));
        BindData();
    }


}
